package com.rd.student.ruanjian.class4.lichunquan.Text02;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;

public class Test08 {
    //20个学生赋值id，成绩，查找成绩最大的，返回id
    public static void main(String[] args) {
        HashSet<Score> hashSet = new HashSet<>();
        Random random =  new Random();
        Iterator<Score> iterator = hashSet.iterator();
        //赋值
        for (int i = 0; i < 20; i++) {
            hashSet.add(new Score(i+1, random.nextInt(100)+1));
        }
        while(true){
            System.out.println("===============");
            System.out.println("功能 1: 查找最大/小值");
            System.out.println("功能 2: 计算平均值");
            System.out.println("功能 3: 按成绩分段");
            System.out.println("===============");
            Scanner sc = new Scanner(System.in);
            switch (sc.nextInt()){
                case 1:
                    //查找最大/小值
                    int max = 0 , min = 100 ;
                    int maxId = 0 , minId = 0 ;
                    for (Score s: hashSet) {
                        if(s.getScore() > max ){max = s.getScore() ; maxId = s.getId();}
                        if(s.getScore() < min ){min  = s.getScore() ; minId = s.getId();}
                    }
                    System.out.println("分数最低的为: " + minId + " 分数为 :" + min);
                    System.out.println("分数最高的为: " + maxId + " 分数为 :" + max);
                    break;
                case 2:
                   //计算平均值
                    int avg = 0;
                    for (Score s: hashSet) { avg += s.getScore(); }
                    System.out.println(avg/hashSet.size());
                    break;
                case 3:
                    //按成绩分段
                    StringBuilder stringBuilder01 = new StringBuilder();
                    StringBuilder stringBuilder02 = new StringBuilder();
                    for (Score s: hashSet){
                        if(s.getScore()>=60){
                            stringBuilder01.append("").append(s.getId()).append("  ").append(s.getScore()).append("\n");
                        }else{
                            stringBuilder02.append("").append(s.getId()).append("  ").append(s.getScore()).append("\n");
                        }
                    }

                    while(true) {
                        System.out.println("===============");
                        System.out.println("功能 1 :大于等于60");
                        System.out.println("功能 2 :小于60");
                        System.out.println("功能 3 :返回上一级");
                        System.out.println("===============");
                        switch (sc.nextInt()) {
                            case 1:
                                System.out.println("=====");
                                System.out.println(stringBuilder01);
                                System.out.println("=====");
                                break;
                            case 2:
                                System.out.println("=====");
                                System.out.println(stringBuilder02);
                                System.out.println("=====");
                                break;
                            case 3:
                                return;
                            default:
                                System.out.println("输入有误！");
                        }
                    }
                default:
                    System.out.println("输入有误！");
            }
        }


    }
}
class Score{
    private int id ;
    private int score;
    public Score(int id, int score) {
        this.id = id;
        this.score = score;
    }
    public int getId() { return id; }
    public int getScore() { return score; }

    @Override
    public String toString() {
        return "Score{" +
                "id=" + id +
                ", score=" + score +
                '}';
    }
}